﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Auditing;
using Abp.Web.Models;
using FwProject.Com;
using FwProject.Com.Dto;
using FwProject.Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace FwProject.Web.Shop.Controllers
{
    public class ContentManageController : FwProjectControllerBase
    {
        private readonly IContentManageAppService _contentManageAppService;
        private readonly IContentTypeAppService _contentTypeAppService;
        public ContentManageController(IContentManageAppService contentManageAppService, IContentTypeAppService contentTypeAppService)
        {
            _contentManageAppService = contentManageAppService;
            _contentTypeAppService = contentTypeAppService;
        }

        #region 内容分类
        public IActionResult ContentType()
        {
            return View();
        }

        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]//关闭ABP的返回数据封装
        public async Task<IActionResult> GetContentTypeData(int page, int limit, string keyword = "")
        {
            var data = await _contentTypeAppService.GetLayerDataAsync(page, limit, keyword);
            return Json(data);
        }

        public IActionResult TypeCreate()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> TypeCreate(ContentTypeEnterDto input)
        {
            var id = await _contentTypeAppService.CreateAsync(input);
            return Json(id);
        }

        public async Task<IActionResult> TypeEdit(Guid id)
        {
            ViewBag.Id = id;
            var entity = await _contentTypeAppService.GetEntityByIdAsync(id);
            return View(entity);
        }

        [HttpPost]
        public async Task<IActionResult> TypeEdit(Guid id, ContentTypeEnterDto input)
        {
            await _contentTypeAppService.ModifyAsync(id, input);
            return Json(id);
        }

        public async Task<IActionResult> TypeDelete(Guid id)
        {
            await _contentTypeAppService.DeleteAsync(id);
            return Json(id);
        }
        #endregion

        #region 内容管理
        public IActionResult Index()
        {
            return View();
        }

        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]//关闭ABP的返回数据封装
        public async Task<IActionResult> GetContentData(int page, int limit, string keyword = "")
        {
            var data = await _contentManageAppService.GetLayerDataAsync(page, limit, keyword);
            return Json(data);
        }

        public async Task<IActionResult> Create()
        {
            var types = await _contentTypeAppService.GetListAsync();
            ViewBag.Types = types.Select(o => new SelectListItem() { Text = o.Name, Value = o.Id.ToString() });
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Create(ContentManageEnterDto input)
        {
            var id = await _contentManageAppService.CreateAsync(input);
            return Json(id);
        }

        public async Task<IActionResult> Details(Guid id)
        {
            var entity = await _contentManageAppService.GetEntityByIdAsync(id);
            return View(entity);

        }


        public async Task<IActionResult> Edit(Guid id)
        {
            ViewBag.Id = id;
            var types = await _contentTypeAppService.GetListAsync();
            ViewBag.Types = types.Select(o => new SelectListItem() { Text = o.Name, Value = o.Id.ToString() });

            var entity = await _contentManageAppService.GetEntityByIdAsync(id);
            return View(entity);
        }

        [HttpPost]
        public async Task<IActionResult> Edit(Guid id, ContentManageEnterDto input)
        {
            await _contentManageAppService.ModifyAsync(id, input);
            return Json(id);
        }

        public async Task<IActionResult> Delete(Guid id)
        {
            await _contentManageAppService.DeleteAsync(id);
            return Json(id);
        }
        #endregion

    }
}